package net.sx.minemodule.mvp.presenters;

import com.alibaba.fastjson.JSON;
import com.kedll.library.mvp.AbstractRefreshPresenter;
import com.kedll.library.mvp.RefreshingView;
import com.kedll.library.util.ToastUtil;

import net.sx.minemodule.MineApiUtils;
import net.sx.minemodule.R;
import net.sx.minemodule.mvp.bean.OrderBean;

import java.util.List;
import java.util.Random;

/**
 * 订单fragment
 *
 * @author 11470
 */
public class MineOrderListPresenter extends AbstractRefreshPresenter<RefreshingView> {
    private int type;
    private Random random;
    private int page = 0, size = 10;

    public MineOrderListPresenter(int type, RefreshingView mView) {
        super(mView);
        this.type = type;
        random = new Random();
        emptyItem.setContent("暂无订单");
        emptyItem.setResId(R.mipmap.img_empty_order);
        adapter.notifyItemChanged(0);
    }

    @Override
    protected void fetchData(boolean isLoadMore) {
        page = isLoadMore ? ++page : 0;
        MineApiUtils.getMyOrderList(type, page, size, jo -> {
            List<OrderBean> list = JSON.parseArray(jo.getItems(), OrderBean.class);
            parserData(list.size() >= size, list);
        }, th -> {
            ToastUtil.showToast(th.getMessage());
        });
    }
}
